﻿from Tkinter import *
import tkSimpleDialog
from tkFileDialog import askopenfilename
from tkFileDialog import asksaveasfilename
import tkMessageBox
    
class AboutDialog(tkSimpleDialog.Dialog):

    def __init__(self, master):
        tkSimpleDialog.Dialog.__init__(self, master)
        
    def body(self, master):        
        Label(master, text="Version: ").grid(row=0, column=0)
        Label(master, text="1.0").grid(row=0, column=1)        

        Label(master, text="Wiki: ").grid(row=1, column=0)
        Label(master, text="http://code.google.com/p/pymapcreator/w/list", justify=LEFT).grid(row=1, column=1)        
        
        Label(master, text="Developers:").grid(row=2, column=0)
        Label(master, text="Bruno Sousa Campos da Costa", justify=LEFT).grid(row=2, column=1)
        Label(master, text="doravante2@gmail.com", justify=LEFT).grid(row=3, column=1)
        Label(master, text="Bruna de Barros Xavier Santos", justify=RIGHT).grid(row=5, column=1)
        Label(master, text="bruninha_221990@yahoo.com.br", justify=LEFT).grid(row=6, column=1)
        

        
    
class OpenDialog:
    def __init__(self):
        self.file = self.open_file()
        
    def open_file(self):
        file_name = askopenfilename(filetypes=[("map","*.map")], defaultextension=".map")
        if file_name == '':
            return None
        return file_name
        
    def get_file(self):
        return self.file
    
class SaveDialog:
    def __init__(self):
        self.file = self.save_file()
        
    def save_file(self):
        file_name = asksaveasfilename(filetypes=[("map","*.map")], defaultextension=".map")
        if file_name == '':
            return None
            
        if not ('.map' in file_name):
            file_name += ".map"
        return file_name
        
    def get_file(self):
        return self.file 
        
class ChangeMapSizeDialog(tkSimpleDialog.Dialog):

    def __init__(self, master, map_size):
        self.map_size = map_size
        self.ok_click = False
        tkSimpleDialog.Dialog.__init__(self, master)

        
    def body(self, master):
        map_width, map_height = self.map_size
        
        Label(master, text="Map Width:").grid(row=0)
        self.map_size_width = Entry(master)
        self.map_size_width.insert(0,str(map_width))       
        self.map_size_width.grid(row=0, column=1)        
        
        Label(master, text="Map Height:").grid(row=1)
        self.map_size_height = Entry(master)
        self.map_size_height.insert(0,str(map_height))       
        self.map_size_height.grid(row=1, column=1)           

        return self.map_size_width # initial focus
        
    def apply(self):
        self.ok_click = True
            
    def validate(self):
        try:           
            map_size_width = int(self.map_size_width.get())
            map_size_height = int(self.map_size_height.get())            
            if map_size_width * map_size_height <= 0:
                raise Exception('Values must be positive integers')              

            self.map_size = (map_size_width, map_size_height)
            return True
            
        except ValueError as value_error:
            tkMessageBox.showwarning('Warning','Values must be integers')
            return False 
        except Exception as e:
            tkMessageBox.showwarning('Warning',str(e))
            print e
            return False             

    def get_map_size(self):
        return self.map_size
        
    def is_ok(self):
        return self.ok_click         

class ChangePaletteSizeDialog(tkSimpleDialog.Dialog):

    def __init__(self, master, palette_size):
        self.palette_size = palette_size
        self.ok_click = False
        tkSimpleDialog.Dialog.__init__(self, master)

        
    def body(self, master):
        palette_width, palette_height = self.palette_size
        
        Label(master, text="Palette Rows:").grid(row=0)
        self.palette_size_width = Entry(master)
        self.palette_size_width.insert(0,str(palette_width))       
        self.palette_size_width.grid(row=0, column=1)        
        
        Label(master, text="Palette Columns:").grid(row=1)
        self.palette_size_height = Entry(master)
        self.palette_size_height.insert(0,str(palette_height))       
        self.palette_size_height.grid(row=1, column=1)         

        return self.palette_size_width # initial focus
        
    def apply(self):
        self.ok_click = True
            
    def validate(self):
        try:           
            palette_size_width = int(self.palette_size_width.get())
            palette_size_height = int(self.palette_size_height.get())            
            if palette_size_width * palette_size_height <= 0:
                raise Exception('Values must be positive integers')            

            self.palette_size = (palette_size_width, palette_size_height)
            return True
            
        except ValueError as value_error:
            tkMessageBox.showwarning('Warning','Values must be integers')
            return False 
        except Exception as e:
            tkMessageBox.showwarning('Warning',str(e))
            print e
            return False             

    def get_palette_size(self):
        return self.palette_size
        
        
    def is_ok(self):
        return self.ok_click        
        

class NewDialog(tkSimpleDialog.Dialog):

    def __init__(self, master, map_size, sprite_size, palette_size, file):
        self.map_size = map_size
        self.sprite_size = sprite_size
        self.file = file
        self.ok_click = False
        tkSimpleDialog.Dialog.__init__(self, master)

        
    def body(self, master):
        map_width, map_height = self.map_size
        sprite_width, sprite_height = self.sprite_size
        
        Label(master, text="Map Width:").grid(row=0)
        self.map_size_width = Entry(master)
        self.map_size_width.insert(0,str(map_width))       
        self.map_size_width.grid(row=0, column=1)        
        
        Label(master, text="Map Height:").grid(row=1)
        self.map_size_height = Entry(master)
        self.map_size_height.insert(0,str(map_height))       
        self.map_size_height.grid(row=1, column=1)         
        
        Label(master, text="Sprite Width:").grid(row=2)
        self.sprite_size_width = Entry(master)
        self.sprite_size_width.insert(0,str(sprite_width))       
        self.sprite_size_width.grid(row=2, column=1)         
        
        Label(master, text="Sprite Height:").grid(row=3)
        self.sprite_size_height = Entry(master)
        self.sprite_size_height.insert(0,str(sprite_height))       
        self.sprite_size_height.grid(row=3, column=1)        

        self.button_ok = Button(master, text="Open", command=self.open_file)
        self.button_ok.grid(row=6,column=0)        
        self.lb_file = Label(master, text=self.file)
        self.lb_file.grid(row=6,column=1)

        return self.map_size_width # initial focus
        
        
    def open_file(self):
        file = askopenfilename(filetypes=[("gif","*.gif")])
        if file <> '' :
            self.file = file
        self.lb_file.config(text=self.file)

        
    def apply(self):
        self.ok_click = True
            
            
    def validate(self):
        try:
            map_size_width = int(self.map_size_width.get())
            map_size_height = int(self.map_size_height.get())
            if map_size_width * map_size_height <= 0:
                raise Exception('Values must be positive integers')

            sprite_size_width = int(self.sprite_size_width.get())
            sprite_size_height = int(self.sprite_size_height.get())
            if sprite_size_width * sprite_size_height <= 0:
                raise Exception('Values must be positive integers')            
                
            self.map_size = (map_size_width, map_size_height)
            self.sprite_size = (sprite_size_width, sprite_size_height)
            
            if self.file == '' :
                raise Exception('Select an image')            
                
            file_tmp = self.file.split('.')
            file_path, file_extention = tuple(file_tmp)
            if file_extention.lower() <> 'gif' :
                raise Exception('Extension not supported')
                
            return True
             
        except Exception as e:
            tkMessageBox.showwarning('Warning',str(e))
            print e
            return False             
            
            
    def get_map_size(self):
        return self.map_size
        
        
    def get_sprite_size(self):
        return self.sprite_size   

        
    def get_file(self):
        return self.file
        
        
    def is_ok(self):
        return self.ok_click